package cn.yllr.tomgxianghui.controller;


import cn.yllr.tomgxianghui.domain.Appointment;
import cn.yllr.tomgxianghui.domain.Order;
import cn.yllr.tomgxianghui.entity.User;
import cn.yllr.tomgxianghui.service.AppointmentService;
import cn.yllr.tomgxianghui.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class AppointmentController {
    @Resource
    private AppointmentService service;

    @Resource
    private OrderService orderService;              //订单接口

    /**
     * 展示用户选择需要给厨师受理的订单
     *
     * @param model
     * @return
     */
    @RequestMapping("/user_selectOrder.html")
    public String showSelectOrder(Model model, HttpSession session, String account) {
        /*有账号这个参数时就把它加入到Session*/
        session.setAttribute("orderCook", account);

        List<Order> findorder = orderService.findorder();                    //使用order接口来查询订单
        model.addAttribute("orders", findorder);
        return "user_center/user_selectOrder";
    }

    /**
     * 生成订单
     *
     * @param orderid
     * @param session
     * @return
     */
    @RequestMapping("/apCooker.do")
    public String apCooker(String orderid, HttpSession session) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String date = df.format(new Date());
        String customerid = (String) session.getAttribute("username");
        String cookid = (String) session.getAttribute("orderCook");
        Boolean flag = service.addAp(orderid, customerid, cookid, date);
        int i = service.updateOrderstatus(orderid);
        return "redirect:/findAppoint.do";
    }

    /**
     * 给用户展示预约信息
     *
     * @param model
     * @param session
     * @return
     */

    @RequestMapping("/findAppoint.do")
    public String findAppoint(Model model, HttpSession session) {
        String username = (String) session.getAttribute("username");
        List<Appointment> list = service.findappointByCid(username);
        User user = orderService.findUserPhoneBysession((String) session.getAttribute("orderCook"));           //调用订单接口的来查询厨师的电话号码
        model.addAttribute("appointments", list);
        model.addAttribute("cooker", user);
        return "user_center/user_appointment";
    }

    /**
     * 给厨师展示预约信息
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/user_cook.html")
    public String findAppointCook(Model model, HttpSession session) {
        String username = (String) session.getAttribute("username");
        List<Appointment> list = service.findappointByCookid(username);
        model.addAttribute("lists", list);
        return "user_center/user_cook";
    }

    /**
     * 厨师同意预约
     *
     * @param orderid
     * @return
     */
    @RequestMapping("/agreeapp")
    public String agreeApp(String orderid, Model model) {
        service.agreeAppoint(orderid);         //同意预约后要把appointment状态改为1
        int i = orderService.agreeAppUpdateOrder(orderid);//同意预约之后把order状态改为1
        System.out.println("i = " + i);
        return "forward:/user_cook.html";
    }

    /**
     * 厨师拒绝邀请
     *
     * @param orderid
     * @return
     */
    @RequestMapping("/refuseapp")
    public String refuseApp(String orderid) {
        service.refuseAppoint(orderid);            //拒绝预约后要把appointment状态改为2
        orderService.refuseAppUpdateOrder(orderid); //拒绝预约后要把order状态改为2
        return "forward:/user_cook.html";
    }

    /**
     * 第一次预约失败，重新预约
     *
     * @param orderid
     * @return
     */
    @RequestMapping("/reAppointment")
    public String reAppointment(String orderid) {
        service.reAppoint(orderid);
        return "redirect:/findAppoint.do";
    }
}
